package com.geek.ui;

import com.geek.busness.config.ConfigLoader;
import com.geek.busness.view.Config;
import com.geek.constant.Constant;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class MainFrame extends JFrame {
	private JPanel centerPanel;
	private JLabel intervalLabel;
	private JTextField intervalTextField;
	private JLabel intervalDesc;
	private JComboBox keySelector;
	private JLabel keyLabel;
	private JLabel errorText;

	public MainFrame(String name){
		super(name);
		this.setContentPane(centerPanel);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.pack();
		this.setVisible(true);
		this.setResizable(false);
		this.setLocation(500,300);

		this.initUIComponents();

		this.initEventListener();
	}

	/**
	 * 自定义UI组件
	 */
	private void createUIComponents() {
	}

	/**
	 * 初始化UI组件
	 */
	private void initUIComponents() {
		this.initInterval();
		this.initSelector();
	}

	/**
	 * 初始化选项的值
	 */
	private void initInterval() {
		Config config = ConfigLoader.load();
		int interval = config.getInterval();
		intervalTextField.setText(interval+"");
	}

	/**
	 * 初始化下拉框的值
	 */
	private void initSelector(){
		Config config = ConfigLoader.load();
		//遍历所有的可选按键，设置到selector里面，并根据配置设置默认值
		Map<String, Integer> map = Constant.KeyMap;
		Iterator<String> iterator = map.keySet().iterator();
		int i=0;
		while (iterator.hasNext()){
			String key = iterator.next();
			Integer code = map.get(key);
			keySelector.addItem(key);
			if(code.equals(config.getCurKeyCode())){
				keySelector.setSelectedIndex(i);
			}
			i++;
		}

	}

	/**
	 * 初始化监听器
	 */
	private void initEventListener() {
		keySelector.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				String item = (String)e.getItem();
				Integer keyCode = Constant.KeyMap.get(item);
				Config config = ConfigLoader.load();
				config.setCurKeyCode(keyCode);
			}
		});
		intervalTextField.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}
			@Override
			public void keyPressed(KeyEvent e) {
			}
			@Override
			public void keyReleased(KeyEvent e) {
				String text = intervalTextField.getText();
				if(!text.isEmpty()){
					Matcher matcher = Pattern.compile("[^0-9]").matcher(text);
					String result = matcher.replaceAll("").trim();
					if(!result.isEmpty()){
						Integer integer = Integer.valueOf(result);
						Config config = ConfigLoader.load();
						config.setInterval(integer);
						intervalTextField.setText(integer+"");
					}else{
						intervalTextField.setText("");
					}
				}
			}
		});
	}

	/**
	 * 更新页面
	 */
	public void updateWindow(){
		Config config = ConfigLoader.load();
		intervalTextField.setText(config.getInterval()+"");

		boolean isStarted = config.isStarted();
		intervalTextField.setEnabled(!isStarted);
		keySelector.setEnabled(!isStarted);

	}


	public static void main(String[] args) {
		new MainFrame("鼠标连点工具");
	}
}
